﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 学生选课及成绩查询管理系统
{
    public partial class UpdatePwd : Form
    {
        public UpdatePwd()
        {
            InitializeComponent();
        }

        User user;

        internal User User
        {
            get { return user; }
            set { user = value; }
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            string uid = user.Userid;
            if (string.IsNullOrEmpty(txt_newpwd.Text) || string.IsNullOrEmpty(txt_repwd.Text))
            {
                MessageBox.Show("信息输入不完整");
                return;
            }
            if (txt_newpwd.Text != txt_repwd.Text)
            {
                MessageBox.Show("密码不一致");
                return;
            }
            if (txt_newpwd.Text.Count() < 8 || txt_newpwd.Text.Count()>16)
            {
                MessageBox.Show("请输入密码长度为8-16位的字符！");
                return;
            }
            string sql = "update dbo.users set pwd=@pwd where userid=@userid";
            SqlParameter[] arraysql = { new SqlParameter("@pwd", txt_newpwd.Text), new SqlParameter("@userid", uid) };
            int res = DBAccess.GetState(sql, arraysql);
            if (res != 0)
            {
                MessageBox.Show("修改成功");
                Form1 f1 = new Form1(user);
                f1.Show();
                this.ParentForm.Dispose();
                this.Dispose();
            }
            else
            {
                MessageBox.Show("更新失败");
            }
        }

        private void txt_newpwd_Click(object sender, EventArgs e)
        {
            string uid =user.Userid;
            string opwd = txt_oldpwd.Text;
            string sql = "select count(*) from dbo.users where userid=@userid and pwd=@pwd";
            SqlParameter[] arraysql = { new SqlParameter("@userid", uid), new SqlParameter("@pwd", opwd) };

            object o = DBAccess.GetOne(sql, arraysql);
            if ((int)o == 1)
            {
                txt_tishi.Text = "✓";
                txt_tishi.ForeColor = Color.Green;
            }
            else
            {
                txt_tishi.Text = "×";
                txt_tishi.ForeColor = Color.Red;
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
